﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Tool_System_Client
{
    public partial class LoginForm : DevExpress.XtraEditors.XtraForm
    {
        private bool m_showing = true;
        public LoginForm()
        {
            InitializeComponent();
        }
        private void pb_Login_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            Opacity = 0.0;
            Activate();
            Refresh();
            //fadeTimer.Start();
            Refresh();

            tb_Login_US.Focus();
        }

        private void bt_Login_Login_Click(object sender, EventArgs e)
        {
            string sName = tb_Login_US.Text.Trim();
            string sPwd = tb_Login_PS.Text.Trim();

            if (sName.Length <= 0 || sPwd.Length <= 0)
            {
                MessageBox.Show("请完整填写登录信息！");
                return;
            }

            if (DBManager.GetInstance().Login(sName, sPwd))
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("登录失败，请确认账号和密码正确");
            }
        }

        private void fadeTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (m_showing)
            {
                double d = 1000.0 / fadeTimer.Interval / 100.0;
                if (Opacity + d >= 1.0)
                {
                    Opacity = 1.0;
                    fadeTimer.Stop();
                }
                else
                {
                    Opacity += d;
                }
            }
            else
            {
                double d = 1000.0 / fadeTimer.Interval / 100.0;
                if (Opacity - d <= 0.0)
                {
                    Opacity = 0.0;
                    fadeTimer.Stop();
                }
                else
                {
                    Opacity -= d;
                }
            }
        }

//        private void tb_Login_US_TextChanged(object sender, EventArgs e)
//        {
//            if (tb_Login_US.Text.Trim().Length > 3)
//            {
//                string strSQL = "select f_id,((select f_name from TS_S_T_P_Department where f_id=TS_S_T_P_Stuff.f_depid)+'/'+f_name) as f_name from TS_S_T_P_Stuff where f_del=0 and f_workercode like '%" + tb_Login_US.Text.Trim() + "%'";
////                string strSQL = "select f_id,((select f_name from TS_S_T_P_RailwayStations where f_id=TS_S_T_P_Stuff.f_rsid)+'/'+(select f_name from TS_S_T_P_Department where f_id=TS_S_T_P_Stuff.f_depid)+'/'+f_name) as f_name from TS_S_T_P_Stuff where f_del=0 and f_workercode like '%" + tb_Login_US.Text.Trim() + "%'";
//                TS_Win_Class.FillListTypeAct(cb_Login_DepList, strSQL);
//            }
//            else
//            {
//                cb_Login_DepList.DataSource = null;
//                cb_Login_DepList.Items.Clear();
//            }
//        }

        private void cb_Login_DepList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //try
            //{
            //    if (cb_Login_DepList.SelectedIndex > -1)
            //    {
            //        pb_Photo.Image = LoadImage(cb_Login_DepList.SelectedValue.ToString());
            //    }
            //    else
            //    {
            //        pb_Photo.Image = null;
            //    }
            //}
            //catch
            //{
            //}
        }
        private Image LoadImage(string _ID)
        {
            string str = "";
            Image _img = null;
            //str = TS_Win_Class.GetSQlFieldValue("select top 1 f_photo from TS_S_T_P_Stuff_Photo where f_del=0 and f_stuffid=" + _ID, "f_photo");

            //try
            //{
            //    if (str.Length > 100)
            //    {
            //        byte[] bt = Convert.FromBase64String(str);
            //        System.IO.MemoryStream stream = new System.IO.MemoryStream(bt);
            //        Bitmap bitmap = new Bitmap(stream);
            //        _img = bitmap;
            //    }
            //}
            //catch
            //{
            //}
            return _img;
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //string item = "SystemSettingForm";
            //if (TS_Win_Locomotive_MainForm.htActionItem[item] != null)
            //{
            //    try
            //    {
            //        ((SysSetting.SystemSettingForm)TS_Win_Locomotive_MainForm.htActionItem[item]).Focus();
            //        ((SysSetting.SystemSettingForm)TS_Win_Locomotive_MainForm.htActionItem[item]).ShowDialog();
            //    }
            //    catch (Exception){}
            //}
            //else
            //{
            //    SysSetting.SystemSettingForm SystemSettingForm = new SysSetting.SystemSettingForm();
            //    SystemSettingForm.StartPosition = FormStartPosition.CenterScreen;
            //    TS_Win_Locomotive_MainForm.htActionItem.Add(item, SystemSettingForm);
            //    SystemSettingForm.Show();
            //}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
